import 'dart:async';
import 'package:basic_common/common_exports.dart';
import 'package:basic_common/global_getx.dart';
import 'package:flutter/services.dart';

/// 控制器
class MainTabController extends GetxController {
  // 页面下标
  var index = 0.obs;
  //标题名称
  final navTitles = ["首页", "患者", "消息", "我的"];

  /// 记录时间
  int lastTime = 0;

  /// 下载进度
  double downPlan = 0.0;

  /// 是否在下载
  bool isDown = false;

  /// 是否需要更新
  bool isUpdate = false;

  /// 新版本版本号
  String newVersion = '';

  // 是否强制更新
  bool isFource = false;

  /// 下载进度
  void downPlanChange(double val) {
    downPlan = val;
    update(['plan']);
  }

  /// 下载状态
  void isDownChange(bool val) {
    isDown = val;
    update(['isupdate']);
  }

  void isUpdateChange(bool val) {
    isUpdate = val;
  }

  void isFourceChange(bool val) {
    isFource = val;
  }

  @override
  void onInit() async {
    // TODO: implement onInit
    globalGetx.getUpdate(1);

    super.onInit();
    EventBusManager.instance.addListener(EventKeys.logout, () {
      // 移除事件监听
      EventBusManager.instance.removeListener(EventKeys.logout);
      // 跳转登录页面
      Get.offNamedUntil(AppRoutes.login, (route) => false);
    });
  }

  /// 退出应用
  Future<bool> exitApp() async {
    int newTime = DateTime.now().millisecondsSinceEpoch;
    int result = newTime - lastTime;
    lastTime = newTime;
    if (result > 1000) {
      ToastUtils.toast("再次返回退出");
    } else {
      SystemNavigator.pop();
    }
    return false;
  }
}
